1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <cstdio> #include <vector> #include <algorithm> #include <map> #include <set> #include <queue> #define LL long long const int maxn = 1e5 + 5; using namespace std; vector <pair<int, int> > X[maxn], Y[maxn]; set <pair<int, int> > sx[maxn], sy[maxn]; int n, s1, s2, d; int x[maxn], y[maxn], lx[maxn], ly[maxn], ox[maxn], oy[maxn]; LL ans = 0; queue <int> q; bool vis[maxn]; void addx(int l, int f){ set <pair<int, int> >::iterator it = sx[l].lower_bound(make_pair(oy[f] - d, 0)); ans += 1ll * (upper_bound(X[l].begin(), X[l].end(), make_pair(oy[f] + d, n)) - lower_bound(X[l].begin(), X[l].end(), make_pair(oy[f] - d, 0))); if (it == sx[l].end()) return; while (oy[it->second] <= oy[f] + d && it != sx[l].end()){ if (!vis[it->second]) q.push(it->second), vis[it->second] = 1; set <pair<int, int> >::iterator temp = it; ++temp; sx[l].erase(it); it = temp; } } void addy(int c, int f){ set <pair<int, int> >::iterator it = sy[c].lower_bound(make_pair(ox[f] - d, 0)); ans += 1ll * (upper_bound(Y[c].begin(), Y[c].end(), make_pair(ox[f] + d, n)) - lower_bound(Y[c].begin(), Y[c].end(), make_pair(ox[f] - d, 0))); if (it == sy[c].end()) return; while (ox[it->second] <= ox[f] + d && it != sy[c].end()){ if (!vis[it->second]) q.push(it->second), vis[it->second] = 1; set <pair<int, int> >::iterator temp = it; ++temp; sy[c].erase(it); it = temp; } } map <pair<int, int>, int> m; void bfs(){ q.push(s1); vis[s1] = 1; while (!q.empty()){ int cur = q.front(); q.pop(); int l1 = lower_bound(lx + 1, lx + n + 1, ox[cur] - d) - lx; int l2 = lower_bound(lx + 1, lx + n + 1, ox[cur] + d) - lx; int c1 = lower_bound(ly + 1, ly + n + 1, oy[cur] - d) - ly; int c2 = lower_bound(ly + 1, ly + n + 1, oy[cur] + d) - ly; bool f1 = (lx[l1] == ox[cur] - d), f2 = (lx[l2] == ox[cur] + d); bool f3 = (ly[c1] == oy[cur] - d), f4 = (ly[c2] == oy[cur] + d); ans -= m[make_pair(l1, c1)] * f1 * f3; ans -= m[make_pair(l2, c1)] * f2 * f3; ans -= m[make_pair(l1, c2)] * f1 * f4; ans -= m[make_pair(l2, c2)] * f2 * f4; if (f1) addx(l1, cur); if (f2) addx(l2, cur); if (f3) addy(c1, cur); if (f4) addy(c2, cur); } } int _abs(int x){ return x < 0 ? -x : x; } int main(){ scanf("%d%d%d", &n, &s1, &s2); for (int a, b, i = 1; i <= n; i++){ scanf("%d%d", &a, &b); x[i] = a - b; y[i] = a + b; ox[i] = x[i], oy[i] = y[i]; lx[i] = x[i], ly[i] = y[i]; } sort(lx + 1, lx + n + 1); sort(ly + 1, ly + n + 1); for (int i = 1; i <= n; i++){ x[i] = lower_bound(lx + 1, lx + n + 1, x[i]) - lx; y[i] = lower_bound(ly + 1, ly + n + 1, y[i]) - ly; X[x[i]].push_back(make_pair(oy[i], i)); Y[y[i]].push_back(make_pair(ox[i], i)); sx[x[i]].insert(make_pair(oy[i], i)); sy[y[i]].insert(make_pair(ox[i], i)); m[make_pair(x[i], y[i])]++; } for (int i = 1; i <= n; i++) sort(X[i].begin(), X[i].end()); for (int i = 1; i <= n; i++) sort(Y[i].begin(), Y[i].end()); d = max(_abs(ox[s1] - ox[s2]), _abs(oy[s1] - oy[s2])); bfs(); printf("%lld\n", ans / 2ll); return 0; }
|